"""
@filename: registerapi.py
@project: flask_project
@author: wlf
@time: 2022-09-07
"""
from flask_restful import Resource, Api
from flask import Blueprint, request, session
from model import db
from libs.response import generate_response
from forms.user import UserForm, LoginForm
from model.user import UserManager

register_bp = Blueprint("registerapi", __name__, url_prefix="/v1")
api = Api(register_bp)


class RegisterView(Resource):
    def post(self):
        data = request.json
        form = UserForm(data=data)
        if form.validate():
            user = UserManager()
            user.create_user(data["username"], data["password"], data["cellphone"], data["school"], data["admission_date"])
            return generate_response(message="user register success")
        else:
            return generate_response(status_code=10001, message=form.errors)


api.add_resource(RegisterView, "/register")


def index():
    str = """This is the campus universal wall,
Here, you can boldly declare, wantonly ridicule; You can also publish found and lost property notices; You can also find someone to share a car with, You can also post second-hand things.
Thank you for your use and support!!! """

    return generate_response(message=str)


register_bp.add_url_rule("/", view_func=index)
